package drds.connection_pool;

import java.util.concurrent.ScheduledExecutorService;

/**
 * A factory for {@link ProxyLeakRunnable} Runnables that are scheduled in the future to report leaks.
 */
class ProxyLeakTaskFactory
{
   private ScheduledExecutorService scheduledExecutorService;
   private long leakDetectionThreshold;

   ProxyLeakTaskFactory(final long leakDetectionThreshold, final ScheduledExecutorService scheduledExecutorService)
   {
      this.scheduledExecutorService = scheduledExecutorService;
      this.leakDetectionThreshold = leakDetectionThreshold;
   }

   ProxyLeakRunnable schedule(final PoolEntry poolEntry)
   {
      return (leakDetectionThreshold == 0) ? ProxyLeakRunnable.NO_LEAK : scheduleNewTask(poolEntry);
   }

   void updateLeakDetectionThreshold(final long leakDetectionThreshold)
   {
      this.leakDetectionThreshold = leakDetectionThreshold;
   }

   private ProxyLeakRunnable scheduleNewTask(PoolEntry poolEntry)
   {
      ProxyLeakRunnable proxyLeakRunnable = new ProxyLeakRunnable(poolEntry);
      proxyLeakRunnable.schedule(scheduledExecutorService, leakDetectionThreshold);

      return proxyLeakRunnable;
   }
}
